# plugs/list.py
#
#

""" maintain lists """

from gozerbot.pdol import Pdol
from gozerbot.datadir import datadir
from gozerbot.commands import cmnds
from gozerbot.plughelp import plughelp
from gozerbot.examples import examples

plughelp.add('list', 'maintain lists')

lists = Pdol(datadir + '/lists')

def handle_lists(bot, ievent):
    """ lists <listname> [',' <item>] """
    if not ievent.rest:
        ievent.missing("<listname> [',' <item>]")
        return
    try:
        listname, item = ievent.rest.split(',', 1)
    except ValueError:
        teller = 0
        resultstr = ""
        listname = ievent.rest.strip()
        list = lists[listname]
        if not list:
            ievent.reply('no %s list available' % listname)
            return
        for i in lists[listname]:
            resultstr += "%s) %s " % (teller, i)
            teller += 1
        ievent.reply(resultstr)
        return
    listname = listname.strip().lower()
    item = item.strip()
    if not listname or not item:
        ievent.missing("<listname> [',' <item>]")
        return
    lists[listname] = item
    lists.save()
    ievent.reply('%s added to %s list' % (item, listname))

cmnds.add('lists', handle_lists, 'USER')
examples.add('lists', "lists <listname> [',' <item>] .. show content of list \
or add item to list", '1) lists bla 2) lists bla, mekker')

def handle_listsdel(bot, ievent):
    """ list-del <listname> ',' <listofnrs> .. remove items with indexnr 
        from list """
    if not ievent.rest:
        ievent.missing('<listname>, <listofnrs>')
        return
    try:
        listname, listofnrs = ievent.rest.split(',', 1)
    except ValueError:
        ievent.missing('<listname>, <listofnrs>')
        return
    listname = listname.strip().lower()
    list = lists[listname]
    if not list:
        ievent.reply('no %s list avaiable' % listname)
        return
    try:
        nrs = []
        for i in listofnrs.split():
            nrs.append(int(i))
    except ValueError:
        ievent.reply('%s is not an integer' % i)
        return
    nrs.sort()
    nrs.reverse()
    itemsdeleted = 0
    for i in range(len(lists.data[listname])-1, -1 , -1):
        if i in nrs:
            try:
                del lists.data[listname][i]
                itemsdeleted += 1
            except IndexError:
                pass
    if not lists[listname]:
        del lists[listname]
        ievent.reply('%s list removed' % listname)
        lists.save()
        return
    lists.save()
    ievent.reply('%s item(s) removed from %s list' % (itemsdeleted, listname))

cmnds.add('lists-del', handle_listsdel, 'USER')
examples.add('lists-del', "lists-del <listname> ',' <listofnrs> .. remove \
items with indexnr from list", '1) list-del mekker , 1 2) list-del mekker , \
0 3 6')

def handle_listsshow(bot, ievent):
    """ show avaiable lists """
    l = lists.data.keys()
    if not l:
        ievent.reply('no lists available')
        return
    else:
        ievent.reply(' .. '.join(l))

cmnds.add('lists-show', handle_listsshow, 'USER')
examples.add('lists', 'show available global lists' , 'lists-show')
